package jsyf.oa.web.system;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import jsyf.com.util.BaseUtils;
import jsyf.com.util.FastJsonConvert;
import jsyf.com.util.ResponseUtils;
import jsyf.oa.facade.system.RoleFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Created by Administrator on 2017/12/7 0007.
 */
@Api(value = "职位接口", description = "职位接口")
@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private RoleFacade roleFacade;

    @RequestMapping(value = "/save.json", method = RequestMethod.POST)
    public void save(HttpServletResponse response, @RequestBody String jsonData) {
        String result = roleFacade.save(jsonData);
        if (!StringUtils.isEmpty(result)) {
            JSONObject jsonResult = new JSONObject();
            jsonResult.put("id", result);
            ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(jsonResult));
        } else {
            ResponseUtils.putJsonResponse(response, BaseUtils.errorrRespond("添加失败！"));
        }
    }

    @RequestMapping(value = "/delById.json", method = RequestMethod.POST)
    public void delById(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String result = roleFacade.delById(jsonParam.getString("id"));
        ResponseUtils.putJsonResponse(response, result);
    }

    @RequestMapping(value = "/getByUserId.json", method = RequestMethod.POST)
    public void getByUserId(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        List<JSONObject> result = roleFacade.getByUserId(jsonParam.getString("userId"));
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessArrRespond(result));
    }

    @RequestMapping(value = "/saveTargetNumber.json", method = RequestMethod.POST)
    public void saveTargetNumber(HttpServletResponse response, @RequestBody String jsonData) {
//        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String result = roleFacade.saveTargetNumber(jsonData);
        JSONObject objResult = new JSONObject();
        objResult.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

    @RequestMapping(value = "/sort.json", method = RequestMethod.POST)
    public void sort(HttpServletResponse response, @RequestBody String jsonData) {
        JSONObject jsonParam = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        Boolean result = roleFacade.sort(jsonParam.getString("roleIds"));
        JSONObject objResult = new JSONObject();
        objResult.put("success", result);
        ResponseUtils.putJsonResponse(response, BaseUtils.succsessObjRespond(objResult));
    }

}
